package com.dmgo.common;

import javax.persistence.*;

import static javax.persistence.GenerationType.IDENTITY;

/**
 * Created by user on 01.11.2014.
 */

@Entity
@Table(name = "train", catalog = "mkyong")
public class Train {
    private int trainId;
    private Station station;

    public Train() {
    }

    public Train(Station station) {
        this.station = station;
    }

    public void setTrainId(int trainId) {
        this.trainId = trainId;
    }

    public void setStation(Station station) {
        this.station = station;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "TRAIN_ID", unique = true, nullable = false)
    public int getTrainId() {
        return trainId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "STATION_ID", nullable = false)
    public Station getStation() {
        return station;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Train train = (Train) o;

        if (trainId != train.trainId) return false;
        if (station != null ? !station.equals(train.station) : train.station != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = trainId;
        result = 31 * result + (station != null ? station.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Train{" +
                "trainId=" + trainId +
                ", station=" + station +
                '}';
    }


}
